class Query

  def initialize(table_name)
    @table_name=table_name
    @criteries = { }
    @foreign_keys = { }
  end

  def foreign_keys=(keys)
    @foreign_keys = keys
  end

  def foreign_keys
    @foreign_keys
  end

  def get_foreign(al)
    foreign = nil
    @foreign_keys.each do |foreign_, props|
      if props[:alias]==al
        foreign = foreign_
      end
    end
    return foreign
  end

  def table_name
    @table_name
  end

  def criteries
    @criteries
  end

  def criteries=(criteries)
    @criteries=criteries
  end

  def criteries_list
    @criteries.map{|name,c| c}
  end

  def <<(criteria)
    @criteries[criteria.name]=criteria
  end

  def del(criteria_name)
    @criteries.delete(criteria_name)
  end

  def fill(request)
    @criteries.each do |name, criteria|
      criteria.from request
    end
  end

  def to_sql
    criteries_list.map{ |c| c.to_sql}.join(' and ')
  end

end
